import pygame
from game_event_manager import GameEventManager
from gameworld import GW

class Score:
    def __init__(self):
        self.score = 0
        self.text = f"{self.score}"
        self.font = pygame.font.SysFont("微软雅黑", 60, "bold")
        self.surf = self.font.render(self.text, True, GW.COLOR_SCORE)
        self.rect = self.surf.get_rect(
            center=(GW.BOARD_LEFT + 13 * GW.BLOCK_SIZE, 80)
        )
        self.visible = True

        GameEventManager.register_handler(GW.EVENT_SCORE_CHANGE, self.refresh)

    def refresh(self, event=None):
        score = event.data.get("score")
        if score:
            self.score = score
            self.text = f"{self.score}"
            self.surf = self.font.render(self.text, True, GW.COLOR_SCORE)

    def show(self, event=None):
        self.visible = True

    def hide(self, event=None):
        self.visible = False


    def draw(self, screen):
        if self.visible:
            screen.blit(self.surf, self.rect)
